Home:ALL Converter>What methods can I call on quote->getAllItems()?

What methods can I call on quote->getAllItems()?

Ask Time:2012-06-22T04:37:53         Author:pokero

Json Formatter

This is proving impossible to find googling, so would appreciate if someone could help me out with this one. I'm relatively new to Magento, so its possible there's official documentation about this even.

When I have the following, which is grabbing the quote object, and then calling getAllItems() on it, where can I see all the methods I can call on getAllItems()?

$cart = Mage::getModel('checkout/cart')->getQuote();

foreach ($cart->getAllItems() as $item) {
    $productId = $item->getProduct()->getId();
    $productPrice = $item->getProduct()->getPrice();
}

I.e. I have seen getId() etc., but what other methods can I use?

And further, if I wanted to have custom data in the quote object, where would I create the custom get method to access this data?

Thanks Paul

Author:pokero,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11146347/what-methods-can-i-call-on-quote-getallitems
Jürgen Thelen :

Getting class methods\nIf you just want to know which methods you can call on $item, use\nvar_dump(get_class_methods($item));\n\nto get a list of class methods.\nIdentifying class file\nIf you want to know which file contains the class definition of $item, first use\nvar_dump(get_class($item));\n\nto get the class name (Mage_Sales_Model_Quote_Item in this case).\nThen replace all underscores (_) by slashes (/) and append .php to it. This will give you the relative file path of the class file (Mage/Sales/Model/Quote/Item.php in this case).\nNow check the folders\napp/code/local/\napp/code/community/\napp/code/core/\nlib/\n\nin the order given for the relative path. The first match usually is the class file you want.\nCreating method to access a custom quote attribute\nYou don't have to. Magento will do this for you.\nLike many other Magento models, the quote object is based on Varien_Object. The latter provides a _call() method, one of PHP's magic methods:\npublic function __call($method, $args)\n{\n switch (substr($method, 0, 3)) {\n case 'get' :\n //Varien_Profiler::start('GETTER: '.get_class($this).'::'.$method);\n $key = $this->_underscore(substr($method,3));\n $data = $this->getData($key, isset($args[0]) ? $args[0] : null);\n //Varien_Profiler::stop('GETTER: '.get_class($this).'::'.$method);\n return $data;\n\n case 'set' :\n //Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method);\n $key = $this->_underscore(substr($method,3));\n $result = $this->setData($key, isset($args[0]) ? $args[0] : null);\n //Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method);\n return $result;\n\n case 'uns' :\n //Varien_Profiler::start('UNS: '.get_class($this).'::'.$method);\n $key = $this->_underscore(substr($method,3));\n $result = $this->unsetData($key);\n //Varien_Profiler::stop('UNS: '.get_class($this).'::'.$method);\n return $result;\n\n case 'has' :\n //Varien_Profiler::start('HAS: '.get_class($this).'::'.$method);\n $key = $this->_underscore(substr($method,3));\n //Varien_Profiler::stop('HAS: '.get_class($this).'::'.$method);\n return isset($this->_data[$key]);\n }\n throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");\n}\n\nNow when you want to have custom data in your quote object, you just call\n$quote->setMyCustomAttribute(911);\n\nUpon such call PHP will check the quote object for a defined method setMyCustomAttribute(). If it cannot find such method, it checks whether the object has a magic __call() method and calls this one instead.\nIn our example, the set case of __call() will be matched.\nWhat happens now is, that Magento transforms the camel-case string setMyCustomAttribute to the underscored key my_custom_attribute first. Then it will store the value 911 to a protected property Varien_Object::_data['my_custom_attribute'].\nTo read custom data from the quote object, you can simply call\n$value = $quote->getMyCustomAttribute();\n\nThe principle is the same: getMyCustomAttribute gets transformed to my_custom_attribute and the current value of Varien_Object::_data['my_custom_attribute'] will be returned.\nThat's all the magic.\nBe aware though, that this is just temporary. If you want to be able to save/load your custom attributes, you need to extend the quote object. But that's another story.",
2012-06-22T09:42:04
Drew Hunter :

The type of a quote item is Mage_Sales_Model_Quote_Item\n\nYou can confirm this by doing: get_class($item)\n\nSo your investigation will begin here: app/code/core/mage/sales/model/quote/item.php\n\nNote: A very useful and much used method in there is getProduct() which will return the product that the quote item represents. It will give you a model of type Mage_Catalog_Model_Product and therefore access to the product attributes which can be very useful in a lot of situations. \n\nAlso notable is the getQuote() method. \n\nApart from that just look at the quote item class file and those it extends to discover what is going on and what is available to you.\n\nUltimately, a quote item extends: Mage_Core_Model_Abstract and in turn Varien_Object (lib/varien/object.php)\n\nBy looking at Varien_Object you will find the getData() & setData() methods + the magic getters & setters. These are some of the methods that can be used to get and set attributes on a model (which extends Varien Object) - this should answer the second part of your question.",
2012-06-21T21:26:20
yy